package com.jiuli.liping.test;

import com.jiuli.liping.dao.ProductDao;
import com.jiuli.liping.dao.ProductOrderDao;
import com.jiuli.liping.util.DateUtils;
import com.jiuli.liping.vo.Product;
import com.jiuli.liping.vo.ProductOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Component
public class MyAsyncTask {

    @Autowired
    private ProductOrderDao productOrderDao;

    @Autowired
    private ProductDao productDao;

    @Async
    @Transactional
    public void task1(List<ProductOrder> productOrderList) throws InterruptedException{
        System.out.println("执行异步方法");
        long start = System.currentTimeMillis();
        Thread.sleep(1000*1800);//半小时
        long end = System.currentTimeMillis();

        for (ProductOrder pro: productOrderList) {
            pro.setOrderStatus("2");//超过30分钟冻结订单
            productOrderDao.updateStatusByNum(pro);
            //存货数量返回
            Product pr2 = new Product();
            pr2.setProductNum(pro.getFbk1());
            Product pr3 = productDao.getProductByProductNum(pr2);//商品资料
            int number = Integer.parseInt(pr3.getFbk3())+Integer.parseInt(pro.getAmount());//现有数量
            pr2.setFbk3(number+"");
            int aa = productDao.updateProductByNumber(pr2);
        }
        System.out.println("执行异步方法结束");
    }

    public void task2() throws InterruptedException{
        long start = System.currentTimeMillis();
        Thread.sleep(2000);
        long end = System.currentTimeMillis();
        System.out.println("任务2：耗时"+ (end-start) + "豪秒");
    }

    public void task3() throws InterruptedException{
        long start = System.currentTimeMillis();
        Thread.sleep(3000);
        long end = System.currentTimeMillis();
        System.out.println("任务3：耗时"+ (end-start) + "豪秒");
    }
}
